// pages/editaddress/editaddress.js
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js'); 
//获取编辑信息 修改新增
import { getAddrePost, setUpAddrssData } from '../../config/https'
var qqmapsdk;

import Toast from '../../miniprogram_npm/_vant-weapp@0.5.7@vant-weapp/toast/toast';
const app = getApp()
// var Tool = require('../../config/tool.js');
//计算距离
// let distance = Tool.getLocation(28.62084, 115.925797, 28.65765, 115.877159)
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id:'',          //id
    toaddInfo:'',   //用户是否已经授权
    showCon:'',     //地址授权弹出是否显示
    address:'',     //地址
    status:'',      //判斷是true编辑还是添加
    checked:false,  //是否设置为默认
    areas:'',       //省市区拼接
    street_number:'',//精确到路
    addressName:'',  //当前地址名称拼接在路后面
    street:"",
    lng:'',
    lat:'',
  },
  // 获取当前位置解析位置
  getLocal: function (latitude, longitude) {
    var that = this
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        console.log(res)
        let address = res.result.ad_info.province + res.result.ad_info.city + res.result.ad_info.district+res.result.address_component.street_number + that.data.addressName
        that.setData({
          lng: res.result.location.lng,
          lat: res.result.location.lat,
          areas: res.result.ad_info.province +'|'+ res.result.ad_info.city +'|'+ res.result.ad_info.district,
          street_number: res.result.address_component.street_number,
          address
        })
      },
      fail: function (res) {
        console.log(res)
      },
    });
  },


  //位置授权提示框事件
  changeModalCancel(e) {
    let sure = e.currentTarget.dataset.name
    this.setData({ showCon: false })
    if (sure == "delo") {
    } else {
      // this.setData({ toaddInfo: false, })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    qqmapsdk = new QQMapWX({
      key: app.globalData.tenxunMap //这里自己的key秘钥进行填充
    });
    //判断是编辑还是添加id编辑
    if (options.id){
      this.setData({
        id:options.id,
        status:true
      })
      wx.setNavigationBarTitle({
        title: '编辑地址'
      })
      this.onEidtData();
    }else{
      this.setData({
        status: false
      })
      wx.setNavigationBarTitle({
        title: '新增地址'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    var that = this
    //微信授权地理位置
    wx.getLocation({
      type: 'wgs84',
      //用户成功授权
      success: function (res) {
        console.log("当前位置", res)
        that.setData({ toaddInfo: true })
      }, fail(res) {
        that.setData({ toaddInfo: false })
      }
    })
  },
  //读取编辑数据
  onEidtData(){
    var that = this
    getAddrePost({
      id: that.data.id
    }).then(res=>{
      that.setData({
        checked: res.address.isdefault==0?false:true,
        lat:res.address.lat,
        lng: res.address.lng,
        areas: res.address.province + '|' + res.address.city + '|' + res.address.area,
        street_number: res.address.address,
        address: res.address.province + res.address.city + res.address.area + res.address.address,
        name: res.address.realname,
        mobile: res.address.mobile,
        street: res.address.street,
      })
      console.log(res)
    })
  },
  //监听门牌号
  onPlat(e){
    console.log(e.detail.value)
  },
  //取消或者设为默认地址
  onChange(){
    this.setData({
      checked: !this.data.checked
    })

  },
  //指引授权弹出框
  onWxMap(){
    let that = this
    wx.getSetting({
      success: (res) => {
        if (!res.authSetting['scope.userLocation']) {
          //打开提示框，提示前往设置页面
          that.setData({
            showCon: true
          })
        }
      }
    })
  },
  //跳转内置地图
  onMapAdder(){
    let that = this;
    wx.chooseLocation({
      success(res){
        console.log(res)
        that.getLocal(res.latitude,res.longitude)
        that.setData({ addressName: res.name})
      },fail(res){
       console.log(res)
      }
    })
  },
  // 点击保存
  submit(e){
    if(!e.detail.value.name){
      Toast("请输入姓名~~")
      return
    }
    if(!e.detail.value.mobile){
      Toast("请输入电话~~")
      return
    }
    if (!this.data.street_number){
      Toast("请选择详细地址!!!")
      return
    }
    console.log(e)
    setUpAddrssData({
      id:this.data.id,
      realname: e.detail.value.name,
      mobile: e.detail.value.mobile,
      lng: this.data.lng,
      lat: this.data.lat,
      areas: this.data.areas,
      address: this.data.street_number + this.data.addressName,
      street: e.detail.value.plate,
      isdefault:this.data.checked?'1':'0'
    }).then(res=>{
      wx.navigateBack({
        
      })
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})